package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.bo.ArchivesBo;
import org.dromara.system.utils.SkyEyeExecuteUtil;
import org.dromara.system.utils.SkyEyeProperties;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SkyAccessLogBo;
import org.dromara.system.domain.vo.SkyAccessLogVo;
import org.dromara.system.domain.SkyAccessLog;
import org.dromara.system.mapper.SkyAccessLogMapper;
import org.dromara.system.service.ISkyAccessLogService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 天眼查调用记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-11-07
 */
@RequiredArgsConstructor
@Service
public class SkyAccessLogServiceImpl implements ISkyAccessLogService {

    private final SkyAccessLogMapper baseMapper;

    /**
     * 查询天眼查调用记录
     */
    @Override
    public SkyAccessLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询天眼查调用记录列表
     */
    @Override
    public TableDataInfo<SkyAccessLogVo> queryPageList(SkyAccessLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkyAccessLog> lqw = buildQueryWrapper(bo);
        Page<SkyAccessLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询天眼查调用记录列表
     */
    @Override
    public List<SkyAccessLogVo> queryList(SkyAccessLogBo bo) {
        LambdaQueryWrapper<SkyAccessLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkyAccessLog> buildQueryWrapper(SkyAccessLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkyAccessLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLinkCode()), SkyAccessLog::getLinkCode, bo.getLinkCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLinkUrl()), SkyAccessLog::getLinkUrl, bo.getLinkUrl());
        lqw.eq(bo.getAccessDate() != null, SkyAccessLog::getAccessDate, bo.getAccessDate());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessPerson()), SkyAccessLog::getAccessPerson, bo.getAccessPerson());
        return lqw;
    }

    /**
     * 新增天眼查调用记录
     */
    @Override
    public Boolean insertByBo(SkyAccessLogBo bo) {
        SkyAccessLog add = MapstructUtils.convert(bo, SkyAccessLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改天眼查调用记录
     */
    @Override
    public Boolean updateByBo(SkyAccessLogBo bo) {
        SkyAccessLog update = MapstructUtils.convert(bo, SkyAccessLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkyAccessLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除天眼查调用记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insert(ArchivesBo bo, String path, String result) {
        String access = path + SkyEyeExecuteUtil.assembleParam(bo);
        String skyId = SkyEyeProperties.getSkyId(path);
        Long userId = LoginHelper.getUserId();

        SkyAccessLog accessLog = new SkyAccessLog();
        accessLog.setAccessDate(new Date());
        accessLog.setLinkCode(skyId);
        accessLog.setLinkUrl(access);
        accessLog.setAccessResult(result);
        accessLog.setAccessPerson(new String(String.valueOf(userId)));

        baseMapper.insert(accessLog);
    }
}
